package com.fesine.quartz.chapter14;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;

import java.util.Date;

/**
 * @description: 几种进行job串连调用
 *                  1、使用jobListener处理，
 *                  2、使用scheduler.triggerJob自行调用，
 *                  3、使用jobDataMap调用
 * @author: Fesine
 * @createTime:2016/11/20 19:42
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/20 19:42
 */
public class FirstJob implements Job {
    Log logger = LogFactory.getLog(FirstJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("第一个job开始执行！");
        context.getJobDetail().getJobDataMap().put("JOB_RESULT", new JobResult());
        //Scheduler scheduler = context.getScheduler();
        //triggerJob(scheduler, SecondJob.class);
        //第三种调用
        scheduleNexJob(context);

    }

    //第二种调用
    public void triggerJob(Scheduler scheduler, Class nextJobClass) {
        try {
            JobDetail detail = new JobDetail(nextJobClass.getSimpleName(), null, nextJobClass);
            detail.setDurability(true);
            Trigger trigger = TriggerUtils.makeImmediateTrigger(0, 5000);
            trigger.setName("FireNowTrigger");
            scheduler.scheduleJob(detail, trigger);
            JobResult result = new JobResult();
            result.setFinishedTime(System.currentTimeMillis());
            result.setJobName(detail.getName());
            result.setSuccess(true);
            JobDataMap dataMap = new JobDataMap();
            dataMap.put("JOB_RESULT", result);

            scheduler.triggerJob(nextJobClass.getSimpleName(), null, dataMap);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //第三种调用
    public void scheduleNexJob(JobExecutionContext context) {
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        String nextJob = jobDataMap.getString("nextJob");
        if (nextJob != null && nextJob.length() > 0) {
            try {
                Class jobClass = Class.forName(nextJob);
                scheduleJob(jobClass,context.getScheduler());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private void scheduleJob(Class jobClass, Scheduler scheduler) {
        JobDetail jobDetail = new JobDetail(jobClass.getSimpleName(), null, jobClass);
        JobDataMap jobDataMap = new JobDataMap();
        JobResult result = new JobResult();
        result.setFinishedTime(System.currentTimeMillis());
        result.setJobName(jobDetail.getName());
        result.setSuccess(true);
        jobDataMap.put("JOB_RESULT", result);
        Trigger trigger = TriggerUtils.makeImmediateTrigger(0, 0);
        trigger.setName(jobClass.getSimpleName() + "Trigger");
        trigger.setStartTime(new Date());
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
